package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.mapper.SystemPostMapper;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Autowired
    private  SystemUserMapper userMapper;
    @Autowired
    private SystemPostMapper postMapper;

    //方式1: 通过sql 关联查询
    //方式2: 基于对象
    @Override
    public IPage<SystemUserItemVo> pageItem(IPage<SystemUserItemVo> page, SystemUserQueryVo queryVo) {
        //使用纯对象的写法  先进行分页查询 之后封装数据
        //user表的分页后的数据
        LambdaQueryWrapper<SystemUser> querywrapper = new LambdaQueryWrapper<>();
        querywrapper.like(queryVo.getName()!=null,SystemUser::getName,queryVo.getName())
                .like(queryVo.getPhone()!=null,SystemUser::getPhone,queryVo.getPhone());
        Page<SystemUser>  userPage = new Page<>(page.getCurrent(),page.getSize());
        userPage = userMapper.selectPage(userPage,querywrapper);

        //已经完成了分页查询功能  获取分页后的数据信息
        //要vo的分页数据  差岗位信息
        List<SystemUser> systemUsers= userPage.getRecords();

        List<SystemUserItemVo> list = new ArrayList<>(systemUsers.size());
        systemUsers.forEach(systemUser -> {
            SystemUserItemVo itemVo = new SystemUserItemVo();
            BeanUtils.copyProperties(systemUser,itemVo);

            SystemPost systemPost = postMapper.selectById(itemVo.getPostId());
            itemVo.setPostName(systemPost.getName());

            list.add(itemVo);
        });
            BeanUtils.copyProperties(userPage,page);
            page.setRecords(list);

        return page;
    }

    @Override
    public void saveUser(SystemUser systemUser) {
        if (systemUser.getId() == null){
            //新增操作
            String password = DigestUtils.sha256Hex(systemUser.getPassword());
        systemUser.setPassword(password);
        this.save(systemUser);
    }else{
            //修改操作
            if (StringUtils.hasLength(systemUser.getPassword())){
                String password = DigestUtils.sha256Hex(systemUser.getPassword());
                systemUser.setPassword(password);
            }
            //根据对象中不为null的属性充当条件 password="" 现在mybatisplus 做了set赋值操作
            this.updateById(systemUser);
        }

    }
}




